package com.atguigu.flink.streamapi.trans;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2023/1/13
 * 
 *   将所有的传感器进行vc的累加
 */
public class Demo9_ProcessNoKeyBy
{
    public static void main(String[] args) {
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //整个环境中只有1个
        env.setParallelism(1);
        
                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .process(new ProcessFunction<WaterSensor, String>()
                   {
                       int sum = 0;
                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                           sum += value.getVc();
                           out.collect("总水位:" +sum);
                       }
                   }).setParallelism(1)
                   //局部优先
                   .print().setParallelism(2);
        
                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    }
}
